﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Core.ServiceImpl.Repositories.Interface;

namespace EzDesk.Core.ServiceImpl.Repositories
{
    public partial class PermissionRepository : IPermissionRepository
    {
        EzDeskDataContext _db;

        public PermissionRepository(EzDeskDataContext db)
        {
            this._db = db;
        }

        #region IRepository<Permission> Members

        public Guid Add(Permission model, bool saveChanges)
        {
            _db.ez_Permissions.InsertOnSubmit(
                new ez_Permission()
                {
                    PermissionId = model.Id,
                    AppName = model.AppName,

                    Name = model.Name,
                    Description = model.Description,
                    DefaultDeny = model.DefaultDeny,
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public Permission GetById(Guid id)
        {
            Permission info = null;
            if (id != null)
            {
                info = _db.ez_Permissions.Where(p => p.PermissionId == id)
                    .Select(p => new Permission()
                    {
                        Id = p.PermissionId,
                        AppName = p.AppName,

                        Name = p.Name,
                        Description = p.Description,
                        DefaultDeny = p.DefaultDeny,
                    })
                    .SingleOrDefault();
            }
            return info;
        }

        public IQueryable<Permission> GetAll()
        {
            return _db.ez_Permissions.Select(
                p => new Permission()
                {
                    Id = p.PermissionId,
                    AppName = p.AppName,

                    Name = p.Name,
                    Description = p.Description,
                    DefaultDeny = p.DefaultDeny,
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_Permission entity = _db.ez_Permissions.Where(p => p.PermissionId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_Permissions.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(Permission model, bool saveChanges)
        {
            var entity = _db.ez_Permissions.Where(p => p.PermissionId == model.Id)
                .SingleOrDefault();

            entity.AppName = model.AppName;
            entity.Name = model.Name;
            entity.Description = model.Description;
            entity.DefaultDeny = model.DefaultDeny;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
        #endregion

    }
}
